package de.wiv.tapestryportlet2.pages;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Response;
import org.slf4j.Logger;

/**
 * Start page of application tapestryportlet2.
 */
public class Index {

    @Inject
    private Logger logger;

    @Persist
    @Property
    private String message;

    public Date getCurrentTime() {
        return new Date();
    }

    @OnEvent(value = "simpleEvent")
    public void onSimpleEvent() {
        this.logger.info("SimpleEvent");
        this.message = "You triggered the SimpleEvent";
    }

    @OnEvent(value = "toAbout")
    public Object onToAbout() {
        return About.class;
    }

    @OnEvent(value = "downloadResource")
    public StreamResponse onEventFromDownloadResource() {
        System.out.println("fromdownload");
        return new StreamResponse() {

            @Override
            public String getContentType() {
                // TODO Auto-generated method stub
                return "application/schmock";
            }

            @Override
            public InputStream getStream() throws IOException {
                return new ByteArrayInputStream(new String("hellohello").getBytes());
            }

            @Override
            public void prepareResponse(Response arg0) {
                // TODO Auto-generated method stub

            }

        };
    }

    @OnEvent(value = "redownloadResource")
    public Object onActionFromReDownloadResource() {
        System.out.println("fromredownload");
        //        return Contact.class;
        return new StreamResponse() {

            @Override
            public String getContentType() {
                // TODO Auto-generated method stub
                return "application/zip";
            }

            @Override
            public InputStream getStream() throws IOException {
                return new ByteArrayInputStream(new String("hellohello").getBytes());
            }

            @Override
            public void prepareResponse(Response arg0) {
                // TODO Auto-generated method stub

            }

        };
    }
}
